package com.kyoto.administrator.mvpdemo.mvp.presenter;

import com.kyoto.administrator.mvpdemo.OnLoginFinishedListener;
import com.kyoto.administrator.mvpdemo.baselib.mvpbase.baseImpl.BasePresenterImpl;
import com.kyoto.administrator.mvpdemo.mvp.View.ILoginView;
import com.kyoto.administrator.mvpdemo.mvp.model.LoginImpl;

public class LoginPresenter extends BasePresenterImpl<ILoginView> implements ILoginPresenter,OnLoginFinishedListener {
    private ILoginView loginView;
    private LoginImpl login;

    //重写构造方法 view重写
    public LoginPresenter(ILoginView view) {
        super(view);
        this.loginView =view;
        login =  new LoginImpl();
    }

    @Override
    public void onUsernameError() {
        if (loginView != null){
            loginView.hideProgress();
            loginView.setUsernameError();
        }
    }

    @Override
    public void onPasswordError() {
        if (loginView != null){
            loginView.hideProgress();
            loginView.setPasswordError();
        }
    }

    @Override
    public void onSuccess() {
        if (loginView != null){
            loginView.navigateToHome();
        }
    }

    @Override
    public void validateCredentials(String username, String password) {
        if (loginView != null){
            loginView.showProgress();
        }
        login.login(username,password,this);
    }

    @Override
    public void onDestroy() {
        loginView = null;
    }
}
